<?php

namespace App\Http\Services;

use App\Http\Model\SendPhoneNumber;
use App\Http\Model\SendPhoneUser;
use App\Http\Model\SendPhoneUserSite;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SendPhoneUserSiteServices extends CommonServices
{
    public function __construct()
    {
        $this->site = new SendPhoneUserSite();
        $this->user = new SendPhoneUser();
        $this->phoneNumber = new SendPhoneNumber();
    }


    /*
     * 修改用户资格
     * @param [$phone]   注册电话
     * @param [$sms_code]   发送验证码
     * @param [$password]   注册密码
     * */
    public function createSite($data)
    {
        $phone = $this->phoneNumber->getPhoneStatus($data['phone_id']);
        if (empty($phone)) {
            return false;
        }
        $redis_key = 'SENDPHONE:' . $phone->phone;
        $hashPhone = Redis::hgetall($redis_key);
        if (empty($hashPhone['user_id'])) {
            return false;
        }
        if ($hashPhone['user_id'] == $data['user_id']) {
            DB::beginTransaction();
            $res1 = $this->site->create($data);
            $res2 = $this->phoneNumber->edit($data['phone_id'], $data['user_id']);
            $res3 = $this->user->edit($data['user_id'], $phone->rank, 1);
            if ($res1 && $res2 && $res3) {
                DB::commit();
                //删除redis
                Redis::persist($redis_key);
                return true;
            } else {
                DB::rollback();
                return false;
            }
        }
    }


}